#include "Clue.h"
#include "Engine.h"
#include "StageManager.h"
#include "Inventory.h"

const char Clue::className[] = "Clue";

#define method(class, name) {#name, &class::name}

Lunar<Clue>::RegType Clue::methods[] = {
	method(Clue, load),
	method(Clue, setImg),
	method(Clue, setClueID),
};

Clue::Clue() {
	m_bremove = false;
}

Clue::Clue(lua_State* L) {
	m_bremove = false;
}

Clue::~Clue() {
}

void Clue::update() {
	if(m_bremove) {
		StageManager::getInstance().getRootNode()->removeChild(m_ptransform);
		m_bremove = false;
	}
}

void Clue::init() {
}

int Clue::load(lua_State* L) {
	// Read Clue position
	float x,y,z;
	std::string modelFile = "../data/mesh/";

	int nbArgs = lua_gettop(L);
	if (nbArgs == 4 && lua_isnumber(L,1) && lua_isnumber(L,2) && lua_isnumber(L,3) && lua_isstring(L,4)) {
		x = lua_tonumber(L,1);
		y = lua_tonumber(L,2);
		z = lua_tonumber(L,3);
		modelFile += lua_tostring(L,4);
	}
	else {
		std::cerr << "Clue loading : wrong parameters" << std::endl;
		return 0;
	}

	// Load Clue model
	m_ptransform = new osg::PositionAttitudeTransform;
	osg::Node* clue = osgDB::readNodeFile(modelFile);

	m_ptransform->addChild(clue);
	m_ptransform->setPosition(osg::Vec3(x, y, z));

	// Declare instance of class to record state of keyboard
	m_pkeyState = new KeyboardState();

	// Set up the tank update callback
	//  pass the constructor a pointer to our tank input device state
	//  that we declared above.
	m_ptransform->setUpdateCallback(new ClueActionCallback(m_pkeyState, this));

	// The constructor for our event handler also gets a pointer to
	//   our tank input device state instance
	KeyboardEventHandler* clueEventHandler = new KeyboardEventHandler(m_pkeyState);

	// Add our event handler to the list
	Engine::getInstance().getViewer().addEventHandler(clueEventHandler);

	StageManager::getInstance().getRootNode()->addChild(m_ptransform);

	return 0;
}

int Clue::setImg(lua_State* L) {
	m_simgFile = "../data/image/";
	int nbArgs = lua_gettop(L);
	if (nbArgs == 1 && lua_isstring(L,1)) {
		m_simgFile += lua_tostring(L,1);
		return 0;
	}
	return -1;
}

int Clue::setClueID(lua_State* L) {
	int nbArgs = lua_gettop(L);
	if (nbArgs == 1 && lua_isnumber(L,1)) {
		m_iclueID = lua_tonumber(L,1);
		return 0;
	}
	return -1;
}

void ClueActionCallback::operator()(osg::Node* node, osg::NodeVisitor* nv) {
		osg::PositionAttitudeTransform* pat = dynamic_cast<osg::PositionAttitudeTransform*> (node);
		if(pat) {
			if ((m_pkeyState->m_key == osgGA::GUIEventAdapter::KEY_Space)&&(m_pkeyState->m_keydown)) {
				osg::Vec3d pos = pat->getPosition();
				osg::Vec3d playerPos = StageManager::getInstance().getCurrentStage()->getPlayer()->getPosition();
				float dist = (pos-playerPos).length();

				if(dist < 3.) {
					m_pkeyState->m_keydown = false;

					StageManager::getInstance().getCurrentStage()->getPlayer()->getInventory()->addClue(m_pclue->getClueID(), m_pclue->getImg());

					m_pclue->setRemove(true);
					m_pclue->getTransform()->removeUpdateCallback(this);

					return;
				}
			}
		}
		traverse(node, nv); 
}
